package com.example.beikeyigou;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.beikeyigou.history.HistoryActivity;
import com.example.beikeyigou.login_reg.LoginRegActivity;
import com.example.beikeyigou.login_reg.SelfDataChangeActivity;
import com.example.beikeyigou.login_reg.UserBean;
import com.example.beikeyigou.setting.SettingActivity;
import com.example.beikeyigou.utils.SaveUserUtils;
import com.example.beikeyigou.utils.URLUtils;

import java.io.File;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的碎片
 */
public class MeFragment extends Fragment {
    Button cueBtn, settingBtn, historyBtn;
    TextView cueText, extendTv;
    CircleImageView userImg;

    String imgPath;

    private UserBean.DataBean userData;     // 之前登录时保存的用户信息

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        refreshData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshData();
    }

    public void refreshData() {
        userData = SaveUserUtils.getUserData(getContext());
        if (userData == null) {
            cueText.setText("请先登录");
            extendTv.setText("登录");
            userImg.setImageResource(R.mipmap.user);
            return;
        }
        if (userData.getId() == -1) {
            return;
        }
        imgPath = URLUtils.getImgPath(getContext(), userData.getPhone());
        cueText.setText(userData.getNickname());
        extendTv.setText("编辑资料");

        File file = new File(imgPath);
        if (file.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
            userImg.setImageBitmap(bitmap);
        } else {
            userImg.setImageResource(R.mipmap.user);
        }
    }

    private void initView(View view) {
        cueText = view.findViewById(R.id.frag_me_cueText);
        extendTv = view.findViewById(R.id.frag_me_extendCue);
        cueBtn = view.findViewById(R.id.frag_me_cueBtn);
        settingBtn = view.findViewById(R.id.frag_me_setting);
        userImg = view.findViewById(R.id.frag_me_image);
        historyBtn = view.findViewById(R.id.frag_me_history);

        historyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HistoryActivity.class);
                startActivity(intent);
            }
        });

        cueBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cueText.getText().equals("请先登录")) {
                    Intent intentLogin = new Intent(getActivity(), LoginRegActivity.class);
                    intentLogin.putExtra("flag", 0);
                    startActivity(intentLogin);
                }
                else {
                    Intent intentSelfData = new Intent(getActivity(), SelfDataChangeActivity.class);
                    startActivity(intentSelfData);
                }
            }
        });

        settingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentSetting = new Intent(getActivity(), SettingActivity.class);
                startActivity(intentSetting);
            }
        });
    }
}